package com.atfx.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传工具类
 */
public class MyFileUpload {

    /**
     * 图片上传工具类
     * @param files
     * @return
     */
    public static List<String> upload_image(MultipartFile[] files) {
        //图片上传磁盘路径
        String path = MyPropertyUtil.getProperty("myUpload.properties", "windows_path");
        List<String> list_image = new ArrayList<String>();

        for (int i = 0; i < files.length; i++) {
            if(!files[i].isEmpty()) {
                String originalFileName = files[i].getOriginalFilename();

                //上传图片名称
                String name = System.currentTimeMillis() + originalFileName;
                String upload_name = path + "/" + name;
                try{
                    File newFile = new File(upload_name);
                    //把内存图片写入磁盘中
                    files[i].transferTo(newFile);
                    //把新的图片写入到对象中，方便数据库中更新
                    //itemsCustom.setPic(newFile);
                    list_image.add(name);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list_image;
    }
}
